package com.tzpay.tzpay.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tzpay.tzpay.R;

import butterknife.ButterKnife;
import butterknife.Bind;

/**
 * Created by fengruicong on 15-12-25.
 */
public class KeyboardView extends FrameLayout {
    @Bind(R.id.btn1)
    TextView btn1;
    @Bind(R.id.btn4)
    TextView btn4;
    @Bind(R.id.btn7)
    TextView btn7;
    @Bind(R.id.btnPoint)
    TextView btnPoint;
    @Bind(R.id.btn2)
    TextView btn2;
    @Bind(R.id.btn5)
    TextView btn5;
    @Bind(R.id.btn8)
    TextView btn8;
    @Bind(R.id.btn0)
    TextView btn0;
    @Bind(R.id.btn3)
    TextView btn3;
    @Bind(R.id.btn6)
    TextView btn6;
    @Bind(R.id.btn9)
    TextView btn9;
    @Bind(R.id.btnDel)
    ImageButton btnDel;
    @Bind(R.id.main_keyboard_layout)
    LinearLayout mainKeyboardLayout;
    private KeyboardClickListener keyboardClickListener;

    public KeyboardView(Context context) {
        super(context);
        initView(context);
    }

    public KeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        if (isInEditMode()) {
            return;
        }
    }

    public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        if (isInEditMode()) {
            return;
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_keyboard, this);
        ButterKnife.bind(this, this);
        setListener();
    }

    private void setListener() {
        buttonListener(btn1, "1");
        buttonListener(btn2, "2");
        buttonListener(btn3, "3");
        buttonListener(btn4, "4");
        buttonListener(btn5, "5");
        buttonListener(btn6, "6");
        buttonListener(btn7, "7");
        buttonListener(btn8, "8");
        buttonListener(btn9, "9");
        buttonListener(btn0, "0");
        btnPoint.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (keyboardClickListener != null) {
                            keyboardClickListener.clickPoint();
                        }
                    }

                });
        btnDel.setOnTouchListener(
                new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                if (keyboardClickListener != null) {
                                    keyboardClickListener.clickDelDown();
                                }
                                break;
                            case MotionEvent.ACTION_UP:
                                if (keyboardClickListener != null) {
                                    keyboardClickListener.clickDelUp();
                                }
                                break;
                        }
                        return false;
                    }
                });
    }

    private OnClickListener buttonListener(final View btn, final String number) {
        OnClickListener listener =
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (keyboardClickListener != null) {
                            keyboardClickListener.clickButton(number);
                        }
                    }
                };
        btn.setOnClickListener(listener);
        return listener;
    }

    public void setKeyboardListener(KeyboardClickListener keyboardClickListener) {
        this.keyboardClickListener = keyboardClickListener;
    }

    public interface KeyboardClickListener {
        void clickButton(String number);

        void clickDelDown();

        void clickDelUp();

        void clickPoint();
    }
}
